/**
 * 
 */
package nl.samenloop.bigscreen.communication;

/**
 * @author Bram Wiekens
 * 
 */
public class ClockReader implements Runnable {

	private String url;
	private Result<Long> resultHandler = null;

	public ClockReader(String url) {
		this.url = url;
	}

	public void setResultHandler(Result<Long> resultHandler) {
		this.resultHandler = resultHandler;
	}

	private long readClock() {
		String result = Utils.retrieveData(url);
		long time = 0;

		if (result != null && result.length() > 0) {
			try {

				time = Long.parseLong(result);
			} catch (NumberFormatException nfe) {

			}
		}

		return time;
	}

	public void run() {
		long time = 0;

		do {
			time = readClock();
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} while (time == 0);
		resultHandler.onSucces(time);
	}

}
